<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
	<meta name="author" content="wangyuguo">
	<title>单位销司机卡</title>
	<script language="javascript" src="../script/print.js" type="text/javascript"></script>
	<script language="javascript" src="../script/common.js" type="text/javascript"></script>
	<script language="javascript" src="../script/card.js" type="text/javascript"></script>
	<!--动态js-->
	<script language="javascript" type="text/javascript">
	//页面加载方法
	var qt;	
	var companyData;
	var revCompany;
	var cardPin;
	
	var tabs = {											//不同的tab触发的方法
		"revwithcard":{name:"tab_withcard",text:"无卡注销",tabFunc:showRevWithCard},
		"revcard":{name:"tab_card",text:"有卡注销",tabFunc:showRevCard}
	};
	function load(){
		setPages(["readcardview","detview"]);
		revCompany = getPara("revocatCompany")
		companyData = parent.companyData;					//上级单位数据
		if (!companyData) {showError("单位信息为空！");return;}
		Eui.buildTabs("tabsdiv",tabs);
		
		var companyUser = companyData.cardUser;
		if (companyUser.type != CardUserType.COMPANY){
			showWarning("不是单位帐户！");
			return;
		}
		Common.setValues(companyUserViewForm,CardMgnt.parseCardUser(companyData));
		
		$("tab_withcard").onclick();
		
	}
	
	//刷新待销卡司机列表
	function refreshTable(){
		qt.paramFunc = function(){
			var params = {}
			params.orgUserId = companyData.cardUser ?companyData.cardUser.id:-1;
			params.status = CardUserStatus.OK;
			return params;
		}
		qt.showTable();
	}

	
	//创建无卡注销司机列表
	function createTable(){
		if (!qt){
			var columns = [];
			columns[0] = {type:"checkbox"}
			qt = new QueryTable("unit_driver_manage",null,"revdrivers",columns,{
				buttons:[
					{name:"revdriver",value:"注销司机",actions:revocatWithCard}
				]
			});
		}
		refreshTable();
	}
	
	//显示有卡注销页面
	function showRevCard(){
		$("driversdiv").style.display = "none";
		$("revcarddiv").style.display = "";
		readFormReset();
	}
	
	//显示无卡注销页面
	function showRevWithCard(){
		$("revcarddiv").style.display = "none";
		$("driversdiv").style.display = "";
		createTable();
	}
	
	//无卡注销司机
	function revocatWithCard(){
		var objs = qt.getBoxValue(true);
		if (objs.length<=0){showWarning("请选择需要做无卡注销的司机！");return;}
		if (!showQuestion("是否注销这些司机？")) return;
		var lockList = [];
		var asnList = [];
		for (var i=0;i<objs.length;i++){
			var obj = objs[i];
			asnList.push(obj[5]);
			var cardStatus = obj[12];
			if (cardStatus == CardStatus.OK)
				lockList.push(obj[5]);
		}
		if (lockList.length > 0){
			if (!showQuestion("选择司机中有未挂失的司机，是否挂失？")) return;
			DWR.call("blacklistService.batchReportLockCard",lockList.join(),function(){
				showInformation("司机挂失成功！请于"+companyData.cardLostHours+"小时后进行司机无卡注销！");
				onCancel();
			})
		}else
			DWR.call("cardMgntService.batchRevocatDrivers",asnList.join(),function(data){
				var driverCount = qt.total-asnList.length;
				showInformation("销户成功！" + (revCompany?"还有"+driverCount+"个司机没有注销":""));
				showRevDet(data.logId);
			},{blockScreen:true});
		
	}
	//读司机卡
	function onReadCard(){
		cardPin = null;
		var asn;
		try{
			getCard();
			asn = getAsn();
		}catch(e){showError(e.description);return;}
		
		DWR.call("mgntQueryService.checkRevWithCard",asn,function(data){
			var cardUser = data.cardUser;
			if (cardUser.type != CardUserType.DRIVER){showWarning("不是司机帐户！");return;}
			if (cardUser.orgUser != companyData.cardUser.id) {showWarning("非本单位司机帐户！");return;}
			
			var c = getCardDetail({disBalance:true,disLoyalty:true,grayStatus:true,tradeDate:data.serverTime})
			if (c.grayFund || c.grayPoint){
				showWarning("灰锁状态的卡片不能执行此操作。");return;
			}
			cardPin = c.cardPin;
			var cardObj = CardMgnt.convertCardObject(data,c);
			Common.setValues(readCardForm,cardObj);
			$("revocateCardBtn").disabled = false;
		},{blockScreen : true});
	}
	
	//有卡销司机帐户
	function revocateCard(){
		if (!showQuestion("是否注销该司机卡？")) return;
	
		var asn = readCardForm.asn.value;
		var amount = readCardForm.cardBalance.value;
		var pointAmount = readCardForm.cardLoyaltyBalance.value;
		
		var cardStatus = readCardForm.status.value;
		var blackType = readCardForm.blackType.value;
		
		try{
			if (cardStatus == CardStatus.BLACK && (blackType == MgntOtherConstant.CARD_BLACKTYPE_OFFICIAL || blackType == MgntOtherConstant.CARD_BLACKTYPE_TEMP))
				DWR.syncCall("blacklistService.unlockLostCardNotFee",asn);
			CardMgnt.cardUnload(asn,Format.yuanToFen(amount),Format.yuanToFen(pointAmount),cardPin)
		}catch(e){showError(e.description);return;}
		DWR.call("cardMgntService.cardRevocation",asn,function(data){
			var driverCount = qt.total-1;
			showInformation("销户成功！" + (revCompany?"还有"+driverCount+"个司机没有注销":""));
			switchPage("detview");
			CardMgnt.showPrintView("detview","revocat_org_card_bill",data.logId,"单位卡销卡凭证");
		},{blockScreen:true});
	}
	
	//显示销卡明细,凭证预览
	function showRevDet(logId){
		switchPage("detview");
		CardMgnt.showPrintView("detview","revocat_org_bill",logId,"单位卡销卡凭证");
	}
	
	//重置
	function onCancel(){
		releaseCard();
		if (parent.onCancel)
			parent.onCancel();
	}
	
	//有卡注销form重置
	function readFormReset(){
		readCardForm.reset();
		$("revocateCardBtn").disabled = true;
	}
	//返回
	function back(){
		var driverCount = qt.total-1;
		if (revCompany && driverCount == 0){
			//单位销户时，如果司机都注销完毕，返回单位销户首页，做单位销户，
			//调用父窗口的方法进行页面的跳转，以及按钮的设置
			parent.toRevocateCompany();
		}else{
			switchPage("readcardview");
			refreshTable();
			readFormReset();
		}
	}
	</script>
</head>
<body onload="load()">
	<!--页面导航-->
	<div id="readcardview">
		<div class="form">
			<div class="formTitle">单位帐号信息</div>
			<form id="companyUserViewForm" name="companyUserViewForm">
				<input type="hidden" name="cardType">
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col4 label4">
					<tr>
						<td><label>单位名称</label> <input name="name" type="text" readonly class="readonly"></td>
						<td><label>证件类型</label> <input name="idnoTypeText" type="text" readonly class="readonly"></td>
						<td><label>证&nbsp;件&nbsp;号</label> <input name="idno" type="text" readonly class="readonly"></td>
						<td><label>客户编号</label> <input name="uniqueId" type="text" readonly class="readonly"></td>
					</tr>
					<tr>
						<td><label>开户机构</label> <input name="buildFileStationName" type="text" readonly class="readonly"></td>
						<td><label>状&nbsp;&nbsp;&nbsp;&nbsp;态</label> <input name="statusText" type="text" readonly class="readonly"></td>
						<td><label>客户级别</label> <input name="currentLevelText" type="text" readonly class="readonly"></td>
						<td><label>卡片类型</label> <input name="cardTypeName" type="text" readonly class="readonly"></td>
					</tr>
				</table>
			</form>
		</div>
		<div id="revocate_drivers">
			<div id='tabsdiv' align="left" style="width:97%;">
			</div>
			<!-- 无卡销司机 -->
			<div id="driversdiv" style="margin-top:-10px;">
				<div id="revdrivers"></div>
				<p>
					<button onclick="onCancel()">返 回</button>
				</p>
			</div>
			<!-- 有卡销司机 -->
			<div id="revcarddiv" style="margin-top:-10px;display:none;">
				<form name="readCardForm" id="readCardForm">
					<input type="hidden" name="status">
					<input type="hidden" name="blackType">
					<div class="form">
						<div class="formTitle">卡片信息</div>
						<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="label6">
							<tr>
								<td><label>姓&nbsp;&nbsp;&nbsp;&nbsp;名</label> <input type="text" name="name" readonly class="readonly"></td>
								<td><label>证 件 号</label> <input type="text" name="idno" readonly class="readonly"></td>
							</tr>
							<tr>
								<td><label>卡&nbsp;&nbsp;&nbsp;&nbsp;号</label> <input type="text" name="asn" readonly class="readonly"></td>
								<td><label>有 效 期</label> <input type="text" name="expiredTime" readonly class="readonly"></td>
							</tr>
							<tr>
								<td><label>卡 类 型</label> <input type="text" name="baseCardTypeText" readonly class="readonly"></td>
								<td><label>卡子类型</label> <input type="text" name="subCardType" readonly class="readonly"></td>
							</tr>	
							<tr>
								<td><label>卡片状态</label> <input type="text" name="statusText" readonly class="readonly"></td>
								<td><label>黑名单类型</label> <input type="text" name="blackTypeText" readonly class="readonly"></td>
							</tr>
							<tr>
								<td><label>卡 余 额</label> <input type="text" name="cardBalance" readonly class="readonly"> 元</td>
								<td><label>卡内积分</label> <input type="text" name="cardLoyaltyBalance" readonly class="readonly"> 分</td>							
							</tr>
							<tr>
								<td><label>备用金余额</label> <input type="text" name="spareBalance" readonly class="readonly"> 元</td>
								<td><label>备用积分</label> <input type="text" name="spareLoyaltyBalance" readonly class="readonly"> 分</td>							
							</tr>			
						</table>
					</div>
				</form>
				<p>
					<button id="readCardBtn" onclick="onReadCard()">读 卡</button>
					<button id="revocateCardBtn" onclick="revocateCard()" disabled>注 销</button>
					<button onclick="readFormReset()">重 置</button>
					<button onclick="onCancel()">返 回</button>
				</p>
			</div>
		</div>
	</div>
	<!--凭证页面-->
	<div id="detview" style="display:none;">
	</div>
</body>
</html>